﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BabyFood.Entities;
using BabyFood.Data;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class TinTucND : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string idTt = Request.Params["IdTinTuc"];
            string tieudeTt = Request.Params["TieuDe"];

            if (idTt != null)
            {
                H_CatalogId.Value = Request.Params["IdTinTuc"].ToString();
                View_TinTuc(Convert.ToInt32(H_CatalogId.Value));
            }
        }
        HienThiTinTucOld();
        HienThiLoaiTin();
    }

    protected void View_TinTuc(int idTinTuc)
    {
        TinTuc entTinTuc = DataRepository.TinTucProvider.GetById(idTinTuc);
        int _view = 0;
        try
        {
            if (entTinTuc != null)
            {
                TinTuc entTinTucId = DataRepository.TinTucProvider.GetById(entTinTuc.Id);
                _view = entTinTucId.LuotXem;
                lbLuotxem.Text = entTinTucId.LuotXem.ToString();
                DmSubTinTuc entSubTin = DataRepository.DmSubTinTucProvider.GetById(entTinTucId.IdSubTinTuc);
                DmLoaiTinTuc entNhomTin = DataRepository.DmLoaiTinTucProvider.GetById(entSubTin.IdNhomTinTuc);
                Hyperlink2.NavigateUrl = "~/" + GetTieuDe(entNhomTin.Ten);
                Hyperlink3.NavigateUrl = "~/chude/" + entSubTin.Id + "_" + entSubTin.TenSubTinTuc + ".aspx";
                lblTieuDeTin.Text = entTinTuc.TieuDe;
                littieude.Text = entTinTuc.TieuDe.ToString(); // Tieu de title
                H_TieuDe.Value = entTinTuc.TieuDe;
                lblNoiDungTin.Text = Server.HtmlDecode(entTinTuc.NoiDung);
                H_SubTin.Value = (entTinTucId.IdSubTinTuc).ToString();
                H_LoaiTin.Value = (entTinTucId.IdLoaiTinTuc).ToString();
                int TotalRows = 0;
                DataRepository.DmSubTinTucProvider.GetPaged("IdNhomTinTuc=" + entSubTin.IdNhomTinTuc, "", 0, 0, out TotalRows);
                if (TotalRows > 1)
                {
                    panelnhom.Visible = true;
                    panelnhom2.Visible = false;
                    lblNhomTintop.Text = entNhomTin.Ten;
                }
                else
                {
                    panelnhom.Visible = false;
                    panelnhom2.Visible = true;
                    lblNhomTintop.Text = "";
                    lblsubnhom2.Text = entSubTin.TenSubTinTuc;
                }
                lblSubNhomTin.Text = entSubTin.TenSubTinTuc;
                string testNgay = Common.StandardStringjquery(entTinTuc.NgayCapNhat.ToString("dddd"));
                if (testNgay == "Chủ Nhật")
                    lblNgayCapNhat.Text = Common.StandardStringjquery(entTinTuc.NgayCapNhat.ToString("'' dddd dd/MM/yyyy, HH:mm 'GMT+7'"));
                else
                    lblNgayCapNhat.Text = Common.StandardStringjquery(entTinTuc.NgayCapNhat.ToString("'Thứ' dddd dd/MM/yyyy, HH:mm 'GMT+7'"));


                // Cap nhat luot xem cho Tin tuc
                string query = string.Empty;
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["netTiersConnectionString"].ConnectionString);
                conn.Open();
                _view += 1;
                query = string.Format("UPDATE TinTuc SET LuotXem = '{0}' where Id = '{1}'", _view, idTinTuc);
                SqlCommand sqlCommand = new SqlCommand();
                sqlCommand.Connection = conn;
                sqlCommand.CommandText = query;
                sqlCommand.ExecuteNonQuery();
                conn.Close();
            }
        }
        catch (Exception ex)
        { }
    }

    protected void View_Tintuc_Tieude(string tieude)
    {
        int totalRows = 0;
        string whereClause = " dbo.fuChuyenCoDauThanhKhongDau(TieuDe) like N'%" + tieude + "%'";
        TList<TinTuc> entTintuc = DataRepository.TinTucProvider.GetPaged(whereClause, "Id DESC", 0, 0, out totalRows);

        if (entTintuc.Count > 0)
        {
            TinTuc entTinTucId = DataRepository.TinTucProvider.GetById(entTintuc[0].Id);
            DmSubTinTuc entSubTin = DataRepository.DmSubTinTucProvider.GetById(entTinTucId.IdSubTinTuc);
            DmLoaiTinTuc entNhomTin = DataRepository.DmLoaiTinTucProvider.GetById(entSubTin.IdNhomTinTuc);
            Hyperlink2.NavigateUrl = "~/" + GetTieuDe(entNhomTin.Ten);
            Hyperlink3.NavigateUrl = "~/chude/" + entSubTin.Id + "_" + entSubTin.TenSubTinTuc + ".aspx";
            lblTieuDeTin.Text = entTintuc[0].TieuDe;
            H_TieuDe.Value = entTintuc[0].TieuDe;
            lblNoiDungTin.Text = Server.HtmlDecode(entTintuc[0].NoiDung);
            H_SubTin.Value = (entTinTucId.IdSubTinTuc).ToString();
            H_LoaiTin.Value = (entTinTucId.IdLoaiTinTuc).ToString();
            int TotalRows = 0;
            DataRepository.DmSubTinTucProvider.GetPaged("IdNhomTinTuc=" + entSubTin.IdNhomTinTuc, "", 0, 0, out TotalRows);
            if (TotalRows > 1)
            {
                panelnhom.Visible = true;
                panelnhom2.Visible = false;
                lblNhomTintop.Text = entNhomTin.Ten;
            }
            else
            {
                panelnhom.Visible = false;
                panelnhom2.Visible = true;
                lblNhomTintop.Text = "";
                lblsubnhom2.Text = entSubTin.TenSubTinTuc;
            }
            lblSubNhomTin.Text = entSubTin.TenSubTinTuc;
            string testNgay = Common.StandardStringjquery(entTintuc[0].NgayCapNhat.ToString("dddd"));
            if (testNgay == "Chủ Nhật")
                lblNgayCapNhat.Text = Common.StandardStringjquery(entTintuc[0].NgayCapNhat.ToString("'' dddd dd/MM/yyyy, HH:mm 'GMT+7'"));
            else
                lblNgayCapNhat.Text = Common.StandardStringjquery(entTintuc[0].NgayCapNhat.ToString("'Thứ' dddd dd/MM/yyyy, HH:mm 'GMT+7'"));
        }
    }

    protected void cmdFacebook_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.Redirect("http://www.facebook.com/sharer.php?u=" + HttpContext.Current.Request.Url.AbsolutePath);
        }
        catch (Exception ex)
        { }
    }
    protected void cmdTwiter_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.Redirect("http://twitter.com/home?status=" + HttpContext.Current.Request.Url.AbsolutePath);
        }
        catch (Exception ex)
        { }
    }
    protected void cmdYahoo_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.Redirect("ymsgr:im?+&msg=Xem bài này hay lắm nè: " + HttpContext.Current.Request.Url.AbsolutePath);
        }
        catch (Exception ex)
        { }
    }
    protected void cmdEmail_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.Redirect("https://mail.google.com/mail/?view=cm&fs=1&to&su=" + H_TieuDe.Value + "&body=" + HttpContext.Current.Request.Url.AbsolutePath + "%23.TwF6mDNohJo.gmail&ui=2&tf=1&shva=1");
        }
        catch (Exception ex)
        { }
    }
    protected void cmdZing_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.Redirect("http://link.apps.zing.vn/share?u=" + HttpContext.Current.Request.Url.AbsolutePath + "&images=&media=&width=0&height=0");
        }
        catch (Exception ex)
        { }
    }
    protected void cmdIn_Click(object sender, ImageClickEventArgs e)
    {
        Session["ctrl"] = Panel1;
        ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script language=javascript>window.open('Print.aspx','PrintMe','height=600px,width=800px,scrollbars=1');</script>");
    }

    /// <summary>
    /// Hiển Thị Sub Tin
    /// </summary>
    private void HienThiTinTucOld()
    {
        int TotalRows = 0;
        TList<TinTuc> TList_TinTuc = DataRepository.TinTucProvider.GetPaged("IdSubTinTuc=" + Convert.ToInt32(H_SubTin.Value), "NgayCapNhat DESC", 0, 3, out TotalRows);
        dlTinTuc1ld.DataSource = TList_TinTuc;
        dlTinTuc1ld.DataBind();
    }
    /// <summary>
    /// Hiển thị Loại tin
    /// </summary>
    private void HienThiLoaiTin()
    {
        int TotalRows = 0;
        TList<TinTuc> TList_TinTuc = DataRepository.TinTucProvider.GetPaged("IdLoaiTinTuc=" + Convert.ToInt32(H_LoaiTin.Value), "NgayCapNhat DESC", 0, 3, out TotalRows);
        dlLoaiTinTuc.DataSource = TList_TinTuc;
        dlLoaiTinTuc.DataBind();
    }

    /// <summary>
    /// Handles ItemDataBound, Binds ExchangesTo to the Child DataList
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>    

    public string GetTieuDe(object Char)
    {
        string s = Char.ToString();
        return Common.ConvertToUnSign(s);
    }
    public string GetLienKet()
    {
        return Common.sPathUpLienKet;
    }

}